package com.zzx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzx.domain.*;
import com.zzx.domain.Vo.*;
import com.zzx.domain.tv.AddGoods;
import com.zzx.domain.tv.Attr;
import com.zzx.exception.ExceptionState;
import com.zzx.exception.SystemException;
import com.zzx.mapper.GoodsMapper;
import com.zzx.service.*;
import com.zzx.util.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 商品表(Goods)表服务实现类
 *
 * @author makejava
 * @since 2022-08-02 13:27:03
 */
@Service("goodsService")
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService {

    @Autowired
    private GoodsPicsService goodsPicsService;
    @Autowired
    private GoodsAttrService goodsAttrService;
    /**
     * 获取商品列表
     * @param query
     * @param pagenum
     * @param pagesize
     * @return
     */
    @Override
    public ResponseResult getGoodsList(String query, Integer pagenum, Integer pagesize) {
        Page<Goods> page = new Page<>(pagenum, pagesize);
        LambdaQueryWrapper<Goods> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.hasText(query),Goods::getGoodsName,query);
        page(page,wrapper);
        List<GoodsList> collect = page.getRecords().stream().map(item ->
                new GoodsList(item.getGoodsId(), item.getGoodsName(), item.getGoodsPrice(), item.getHotMumber(), item.getGoodsWeight(), item.getGoodsState(), item.getAddTime(), item.getUpdTime(), item.getHotMumber(), item.getIsPromote() == 0 ? false : true)
        ).collect(Collectors.toList());

        Map map = new HashMap();
        map.put("total",page.getTotal());
        map.put("pagenum",page.getCurrent());
        map.put("goods",collect);
        return new ResponseResult("获取成功", ExceptionState.OK,map);
    }

    /**
     * 添加商品
     * @param addGoods
     * @return
     */
    @Override
    @Transactional
    public ResponseResult addGoods(AddGoods addGoods) {
        String[] split = addGoods.getGoods_cat().split(",");
        Goods g = new Goods(null,addGoods.getGoods_name(),addGoods.getGoods_price(),
                addGoods.getGoods_number(),addGoods.getGoods_weight(),split[split.length-1],
                addGoods.getGoods_introduce(),null,null,"0",
                null,null,null,Long.valueOf(split[0]),Integer.valueOf(split[1]),
                Integer.valueOf(split[1]),0,0,0);
        /*添加商品*/
        boolean save = save(g);
        if(save==false) throw new SystemException(ExceptionState.UNCRESTEROLE);
        /*添加图片*/
        List<Pic> pics = addGoods.getPics();
        Pic pic = null;
        if(pics.size()!=0){
             pic = pics.get(0);
        }
        /*添加商品*/
        GoodsPics goodsPics = new GoodsPics(null,g.getGoodsId(), Objects.isNull(pic)?null:pic.getPic(), Objects.isNull(pic)?null:pic.getPic(), Objects.isNull(pic)?null:pic.getPic());
        boolean save1 = goodsPicsService.save(goodsPics);
        if(save1==false) throw new SystemException(ExceptionState.UNCRESTEROLE);
        /*添加商品参数*/
        List<Attr> attrs = addGoods.getAttrs();
        List<GoodsAttr> collect = attrs.stream().map(item ->
                new GoodsAttr(null, g.getGoodsId(), item.getAttr_id(), item.getAttr_value()==null?"": item.getAttr_value(), 0.0)
        ).collect(Collectors.toList());
        boolean b = goodsAttrService.saveBatch(collect);
        if (b==false) throw new SystemException(ExceptionState.UNCRESTEROLE);
        GoodsVo goodsBYGoodsId = getGoodsBYGoodsId(g.getGoodsId());
        return new ResponseResult("创建商品成功",ExceptionState.CREATED,goodsBYGoodsId);
    }

    /**
     * 通过id获取商品
     * @param id
     * @return
     */
    @Override
    public ResponseResult getGoodsById(Long id) {

        GoodsVo goods = getGoodsBYGoodsId(id);
        return new ResponseResult("超级商品成功",ExceptionState.CREATED,goods);
    }

    /**
     * 根据id删除商品
     * @param id
     * @return
     */
    @Transactional
    @Override
    public ResponseResult deleteGoodsById(Long id) {
        // 获取商品
        Goods byId = getById(id);
        // 获取图片
        LambdaQueryWrapper<GoodsPics> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(GoodsPics::getGoodsId,id);
        List<GoodsPics> list = goodsPicsService.list(wrapper);
        List<Long> collect = list.stream().map(item -> item.getPicsId()).collect(Collectors.toList());
        // 获取属性
        LambdaQueryWrapper<GoodsAttr> wrapper2 = new LambdaQueryWrapper<>();
        wrapper.eq(GoodsPics::getGoodsId,id);
        List<GoodsAttr> list2 = goodsAttrService.list(wrapper2);
        List<Long> collect2 = list2.stream().map(item -> item.getAttrId()).collect(Collectors.toList());

        //获取分类
        /*删除图片*/
        goodsPicsService.removeBatchByIds(collect);
        /*删除属性*/
        goodsAttrService.removeBatchByIds(collect2);
        /*删除商品*/
        removeById(id);
        return new ResponseResult("删除成功",ExceptionState.OK);
    }

    public GoodsVo getGoodsBYGoodsId( Long id){
        /*获取商品信息*/
        Goods goods = getById(id);
        /*获取图片信息*/
        List<GoodsPicsVo> goodsPics = goodsPicsService.getGoodsPicsByGoodsId(id);
        /*获取参数信息*/
        List<GoodsAttrVo> goodsAttr = goodsAttrService.getGoodsAttrByGoodsId(Long.valueOf(id));
        GoodsVo goodsVo = new GoodsVo();
        goodsVo.setGoods_id(goods.getGoodsId());
        goodsVo.setGoods_name(goods.getGoodsName());
        goodsVo.setGoods_price(goods.getGoodsPrice());
        goodsVo.setGoods_number(goods.getGoodsNumber());
        goodsVo.setGoods_weight(goods.getGoodsWeight());
        goodsVo.setGoods_introduce(goods.getGoodsIntroduce());
        goodsVo.setGoods_big_logo(goods.getGoodsBigLogo());
        goodsVo.setGoods_small_logo(goods.getGoodsSmallLogo());
        goodsVo.setGoods_state(goods.getGoodsState());
        goodsVo.setAdd_time(goods.getAddTime());
        goodsVo.setUpd_time(goods.getUpdTime());
        goodsVo.setHot_mumber(goods.getHotMumber());
        goodsVo.setIs_promote(goods.getUpdTime()==0?false:true);
        goodsVo.setPics(goodsPics);
        goodsVo.setAttrs(goodsAttr);
        return goodsVo;
    }
}

